package com.douxg.fastCoding.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douxg.fastCoding.spring.base.MyBaseController;
import com.douxg.fastCoding.spring.vo.Result;
import com.douxg.fastCoding.web.consts.DictType;
import com.douxg.fastCoding.web.entity.Cfg;
import com.douxg.fastCoding.web.entity.Dict;
import com.douxg.fastCoding.web.service.CfgService;
import com.douxg.fastCoding.web.service.DictService;
import com.douxg.fastCoding.web.util.DataConverUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description
 * @Auther douxg
 * @Date 2021/6/4 14:57
 */
@RestController
@RequestMapping("fc/cfg")
public class CfgController extends MyBaseController {
    @Resource
    private CfgService service;
    @Resource
    private DictService dictService;

    @PostMapping("insert")
    public Result insert(@RequestBody Cfg dto) {
        boolean handleResult = service.save(dto);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    public Result update(@RequestBody Cfg dto) {
        boolean handleResult = service.updateById(dto);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    public Result delete(@RequestBody Cfg dto) {
        boolean handleResult = service.removeById(dto.getId());
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("page")
    public Result page(@RequestBody Cfg dto) {
        IPage<Map<String, Object>> mapIPage = service.mySelectMapsPage(dto);
        Map<String, Object> dictMap = dictService.MapByType(new Dict().setType(DictType.CFG_TYPE));
        DataConverUtil.dictReplace(dictMap, mapIPage.getRecords(), Cfg.Column.TYPE, Cfg.Column.TYPE, "");
        return Result.success(mapIPage);
    }
}
